package com.base.sdk.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.ViewConfiguration;

import java.lang.reflect.Field;

/**
 * 设备工具类
 * @author hp
 *
 */
public class DeviceUtil {

	/**
	 * 判断当前网络是否可用
	 * @param context
	 * @return
	 */
	 public static boolean checkNet(Context context) {
	        try {
	            ConnectivityManager connectivity = (ConnectivityManager) context
	                    .getSystemService(Context.CONNECTIVITY_SERVICE);
	            if (connectivity != null) {
	                // 获取网络连接管理的对象
	                NetworkInfo info = connectivity.getActiveNetworkInfo();
	                if (info != null && info.isConnected()) {
	                    // 判断当前网络是否已经连接
	                    if (info.getState() == NetworkInfo.State.CONNECTED) {
	                        return true;
	                    }
	                }
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        return false;
	    }
	 
	 /**
		 * dp转px
		 * @param context
		 * @param dipValue
		 * @return
		 */
		public static int dip2px(Context context, float dipValue) {
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (dipValue * scale + 0.5f);
		}

		/**
		 * px转dp
		 * @param context
		 * @param pxValue
		 * @return
		 */
		public static int px2dip(Context context, float pxValue) {
			final float scale = context.getResources().getDisplayMetrics().density;
			return (int) (pxValue / scale + 0.5f);
		}

		/**
		 * 得到屏幕的宽度（dp）
		 * @param context
		 * @return
		 */
		public static int getScreenWidthDip(Context context) {
			int widthPx = DeviceUtil.getScreenWidth(context);
			int widthDip = DeviceUtil.px2dip(context, widthPx);
			return widthDip;
		}

		/**
		 * 得到屏幕屏幕的宽度（dp）
		 * @param context
		 * @return
		 */
		public static int getScreenHeightDip(Context context) {
			int heightPx = DeviceUtil.getScreenHeight(context);
			int heightDip = DeviceUtil.px2dip(context, heightPx);
			return heightDip;
		}

		/**
		 * 获取设备密度
		 * 
		 * @param context
		 * @return
		 */
		public static float getDensity(Context context) {
			if (context != null) {
				return context.getResources().getDisplayMetrics().density;
			} else {
				return 1.0f;
			}
		}

		/**
		 * 获取设备密度DPI
		 * 
		 * @param context
		 * @return
		 */
		public static int getDensityDpi(Context context) {
			if (context != null) {
				return context.getResources().getDisplayMetrics().densityDpi;
			} else {
				return 120;
			}
		}

		/**
		 * 获取屏幕宽度
		 * 
		 * @param context
		 * @return
		 */
		public static int getScreenWidth(Context context) {
			if (context != null) {
				return context.getResources().getDisplayMetrics().widthPixels;
			} else {
				return 0;
			}
		}

		/**
		 * 获取屏幕高度
		 * 
		 * @param context
		 * @return
		 */
		public static int getScreenHeight(Context context) {
			if (context != null) {
				return context.getResources().getDisplayMetrics().heightPixels;
			} else {
				return 0;
			}
		}

		/**
		 * 获取状态栏高度
		 * @param context
		 * @return
		 */
		public static int getStatusBarHeight(Context context) {
			Class<?> c = null;
			Object obj = null;
			Field field = null;
			int x = 0, statusBarHeight = 0;
			try {
				c = Class.forName("com.android.internal.R$dimen");
				obj = c.newInstance();
				field = c.getField("status_bar_height");
				x = Integer.parseInt(field.get(obj).toString());
				statusBarHeight = context.getResources().getDimensionPixelSize(x);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
			return statusBarHeight;
		}
		
		/**
		 * 获得触发移动事件的最短距离
		 * @param context
		 * @return
		 */
		public static float getTouchSlop(Context context) {
			ViewConfiguration configuration = ViewConfiguration.get(context);
			return configuration.getScaledTouchSlop();
		}
	
}
